package com.kucan.recyclecard.fragments;

import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.kucan.recyclecard.R;
import com.kucan.recyclecard.base.BaseFragment;
import com.kucan.recyclecard.bean.BannerBean;
import com.kucan.recyclecard.net.ApiCallBack;
import com.kucan.recyclecard.net.ApiResponse;
import com.kucan.recyclecard.utils.FormatUtil;
import com.kucan.recyclecard.utils.LogUtil;
import com.kucan.recyclecard.utils.Utils;
import com.kucan.recyclecard.view.ImageCycleView;

import java.util.List;

public class HomeFragment extends BaseFragment {
    private ImageCycleView icv_banner;

    @Override
    protected void initVariable() {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_home;
    }

    @Override
    protected void findViews(View rootView) {
        icv_banner = $(R.id.icv_banner);

    }

    @Override
    protected void setListeners() {

    }

    @Override
    protected void refreshDataSource() {
        if (!isHasFetchData()) {
            LogUtil.d("HomeFragment refreshDataSource");
            mApi.getBannerList(new ApiCallBack<ApiResponse<List<BannerBean>>>() {
                @Override
                public void onSuccess(ApiResponse<List<BannerBean>> data) {
                    if (data.data != null){
                        icv_banner.setImageResources(data.data, new ImageCycleView.ImageCycleViewListener() {

                            @Override
                            public void displayImage(String imageURL, ImageView imageView) {
                                RequestOptions requestOptions = new RequestOptions()
                                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                                        .centerCrop()
                                        .transform(new RoundedCorners(FormatUtil.dp2px(mContext, 12)));
                                Glide.with(mContext).load(imageURL).apply(requestOptions).into(imageView);
                            }

                            @Override
                            public void onImageClick(BannerBean info, int position, View imageView) {

                            }
                        });
                    }
                }

                @Override
                public void onFailure(String errorCode, String message) {
                }
            });
        }
    }


}
